package com.suray.wcs.quartz.controller;

import com.suray.commin.annotation.Log;
import com.suray.commin.core.controller.BaseController;
import com.suray.commin.core.controller.ResponseData;
import com.suray.commin.core.page.TableDataInfo;
import com.suray.commin.core.utils.ExcelUtil;
import com.suray.commin.core.utils.SecurityUtils;
import com.suray.commin.enmus.BusinessType;
import com.suray.commin.exception.TaskException;
import com.suray.wcs.quartz.utils.CronUtils;
import com.suray.wcs.quartz.entity.JobDB;
import com.suray.wcs.quartz.service.IJobDBService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 调度任务信息操作处理
 * 
 * @author shaozn
 */
@RestController
@RequestMapping("/job")
public class JobDBController extends BaseController
{
    @Autowired
    private IJobDBService jobDBService;

    /**
     * 查询定时任务列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:list')")
    @GetMapping("/list")
    public TableDataInfo list(JobDB sysJob)
    {
        startPage();
        List<JobDB> list = jobDBService.selectJobList(sysJob);
        return getDataTable(list);
    }

    /**
     * 导出定时任务列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:export')")
    @Log(title = "定时任务", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public ResponseData export(JobDB sysJob)
    {
        List<JobDB> list = jobDBService.selectJobList(sysJob);
        ExcelUtil<JobDB> util = new ExcelUtil<JobDB>(JobDB.class);
        return util.exportExcel(list, "定时任务");
    }

    /**
     * 获取定时任务详细信息
     */
    @GetMapping(value = "/{jobId}")
    public ResponseData getInfo(@PathVariable("jobId") Long jobId)
    {
        return ResponseData.success(jobDBService.selectJobById(jobId));
    }

    /**
     * 新增定时任务
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:add')")
    @Log(title = "定时任务", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseData add(@RequestBody JobDB sysJob) throws SchedulerException, TaskException
    {
        if (!CronUtils.isValid(sysJob.getCronExpression()))
        {
            return ResponseData.error("cron表达式不正确");
        }
        sysJob.setCreateBy(SecurityUtils.getUsername());
        sysJob.setCreateTime(new Date());
        return toAjax(jobDBService.insertJob(sysJob));
    }

    /**
     * 修改定时任务
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:edit')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseData edit(@RequestBody JobDB sysJob) throws SchedulerException, TaskException
    {
        if (!CronUtils.isValid(sysJob.getCronExpression()))
        {
            return ResponseData.error("cron表达式不正确");
        }
        sysJob.setUpdateBy(SecurityUtils.getUsername());
        sysJob.setUpdateTime(new Date());
        return toAjax(jobDBService.updateJob(sysJob));
    }

    /**
     * 定时任务状态修改
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:edit')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public ResponseData changeStatus(@RequestBody JobDB job) throws SchedulerException
    {
        JobDB newJob = jobDBService.selectJobById(job.getJobId());
        newJob.setStatus(job.getStatus());
        return toAjax(jobDBService.changeStatus(newJob));
    }

    /**
     * 定时任务立即执行一次
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:run')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping("/run")
    public ResponseData run(@RequestBody JobDB job) throws SchedulerException
    {
        jobDBService.run(job);
        return ResponseData.success();
    }

    /**
     * 删除定时任务
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:remove')")
    @Log(title = "定时任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jobIds}")
    public ResponseData remove(@PathVariable Long[] jobIds) throws SchedulerException, TaskException
    {
        jobDBService.deleteJobByIds(jobIds);
        return ResponseData.success();
    }
}
